#!/usr/bin/env pybricks-micropython

from pybricks.parameters import Color, Stop,Direction,Button,Port
from pybricks.ev3devices import InfraredSensor
from pybricks.media.ev3dev import Font
from pybricks.tools import wait
import time

class MyScreen:
    ev3 = None 
    myInfrared = None
    speedOption = 1
    wayOption  = 1
    confirmStart = 1
    levelOption = 1
    confirmGo=0

    xlog  = 0
    ylog  = 1
   

    def __init__(self,ev3):
        print("MyRobot starting  ------------")
        # Initialize the motors.
        
        self.ev3 =ev3
        self.myInfrared = InfraredSensor(Port.S3)
        # chinese_font = Font(size=12, lang='zh-cn')
        chinese_font = Font(size=12)
        self.ev3.screen.set_font(chinese_font)
    def showScreen(self):    
        wait(20) 
        x = 35 
        y = 10
        self.ev3.screen.draw_text(x,y,"Robot Match V1.0")
        self.ev3.screen.draw_text(x+18,y+20,"Select Speed")
        if (self.speedOption == 1 ):
            self.ev3.screen.draw_text(x,y+40,"Low",Color.WHITE,Color.BLACK)
        else:
            self.ev3.screen.draw_text(x,y+40,"Low",Color.BLACK,Color.WHITE)
        if (self.speedOption == 2):
            self.ev3.screen.draw_text(x+40,y+40,"Mid",Color.WHITE,Color.BLACK)
        else:
            self.ev3.screen.draw_text(x+40,y+40,"Mid",Color.BLACK,Color.WHITE)
        
        if (self.speedOption == 3 ):
            self.ev3.screen.draw_text(x+80,y+40,"High",Color.WHITE,Color.BLACK)
        else:
            self.ev3.screen.draw_text(x+80,y+40,"High",Color.BLACK,Color.WHITE)
        
        self.ev3.screen.draw_text(x+22,72,"Select Way")
        if (self.wayOption==1):
            self.ev3.screen.draw_text(x,y+80,"One",Color.WHITE,Color.BLACK)
        else:
            self.ev3.screen.draw_text(x,y+80,"One",Color.BLACK,Color.WHITE)

        if (self.wayOption==2):   
            self.ev3.screen.draw_text(x+40,y+80,"Two",Color.WHITE,Color.BLACK)
        else:
            self.ev3.screen.draw_text(x+40,y+80,"Two",Color.BLACK,Color.WHITE)

        if (self.wayOption==3):
            self.ev3.screen.draw_text(x+80,y+80,"Three",Color.WHITE,Color.BLACK)
        else:
            self.ev3.screen.draw_text(x+80,y+80,"Three",Color.BLACK,Color.WHITE)

        if (self.confirmStart==1):
            self.ev3.screen.draw_text(x+30,y+100,"|START|",Color.WHITE,Color.BLACK)
        else:
            self.ev3.screen.draw_text(x+30,y+100,"|START|",Color.BLACK,Color.WHITE)
    
        if (self.levelOption==1):
            self.ev3.screen.draw_box(x-2,y+38, x+8+100,y+44+10, 0, False, color=Color.BLACK)
            self.ev3.screen.draw_box(x-2,y+78, x+8+100+10,y+44+10+40, 0, False, color=Color.WHITE)  
            self.ev3.screen.draw_box(x-2,y+98, x+8+100,y+98+14, 0, False, color=Color.WHITE) 
        if (self.levelOption==2):
            self.ev3.screen.draw_box(x-2,y+38, x+8+100,y+44+10, 0, False, color=Color.WHITE)
            self.ev3.screen.draw_box(x-2,y+78, x+8+100+10,y+44+10+40, 0, False, color=Color.BLACK)  
            self.ev3.screen.draw_box(x-2,y+98, x+8+100,y+98+14, 0, False, color=Color.WHITE) 
        if (self.levelOption==3):
            self.ev3.screen.draw_box(x-2,y+38, x+8+100,y+44+10, 0, False, color=Color.WHITE)
            self.ev3.screen.draw_box(x-2,y+78, x+8+100+10,y+44+10+40, 0, False, color=Color.WHITE)  
            self.ev3.screen.draw_box(x-2,y+98, x+8+100,y+98+14, 0, False, color=Color.BLACK) 

    def monitorButton(self):
       
        if Button.UP in self.ev3.buttons.pressed() or Button.LEFT_UP in self.myInfrared.buttons(1):
            print("--up---")
            if (self.levelOption <=1):
                self.levelOption = 1
            else:
              self.levelOption -= 1
            while Button.UP in self.ev3.buttons.pressed() or Button.LEFT_UP in self.myInfrared.buttons(1):
                wait(10)
            self.paySound(['F4/8']) 

        if Button.DOWN in self.ev3.buttons.pressed() or Button.LEFT_DOWN in self.myInfrared.buttons(1):
            print("--down---")
            if (self.levelOption >=3):
                self.levelOption = 3
            else:
              self.levelOption += 1
            while Button.DOWN in self.ev3.buttons.pressed() or Button.LEFT_DOWN in self.myInfrared.buttons(1):
                wait(10)
            self.paySound(['F4/8']) 

        if Button.LEFT in self.ev3.buttons.pressed() or Button.RIGHT_UP in self.myInfrared.buttons(1):
            print("--left---")
            if(self.levelOption == 1):
                if (self.speedOption >1):                    
                    self.speedOption -=1
            if(self.levelOption == 2):
                if (self.wayOption >1):      
                    self.wayOption -=1
            if(self.levelOption == 3):
                if (self.confirmStart ==1):  
                    self.confirmStart =0
                else:
                    self.confirmStart =1
            while Button.LEFT in self.ev3.buttons.pressed() or Button.RIGHT_UP in self.myInfrared.buttons(1):
                wait(10)
            self.paySound(['F4/8']) 

        if Button.RIGHT in self.ev3.buttons.pressed() or Button.RIGHT_DOWN in self.myInfrared.buttons(1):
            print("--right---")
            if(self.levelOption == 1):
                if (self.speedOption <3):                    
                    self.speedOption +=1
            if(self.levelOption == 2):
                if (self.wayOption <3):      
                    self.wayOption +=1
            if(self.levelOption == 3): 
                if (self.confirmStart ==1):  
                    self.confirmStart =0
                else:
                    self.confirmStart =1
            while Button.RIGHT in self.ev3.buttons.pressed() or Button.RIGHT_DOWN in self.myInfrared.buttons(1):
                wait(10)
            self.paySound(['F4/8']) 

        if Button.CENTER in self.ev3.buttons.pressed() or Button.BEACON in self.myInfrared.buttons(1):
            print("--cent---")
            if (self.confirmStart == 1):
                self.confirmGo = 1
            while Button.CENTER in self.ev3.buttons.pressed() or Button.BEACON in self.myInfrared.buttons(1):
                wait(10)
            self.paySound(['F4/8']) 

    
    def paySound(self,sound):
        self.ev3.speaker.play_notes(sound)
 